﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace hse_2013_local_game2d
{
    public class Ship
    {
        public delegate void ShipEvents(Ship s);
        public static event ShipEvents ShipHit;
        private int sinkWay;
        private bool onFire;
        public bool OnFire
        {
            get { return onFire; }
            set { onFire = value; }
        }
        private Wave baseWave;
        private Point position;
        public Point Position
        {
            get { return position; }
        }
        private Image shipView;
        private Image afterExplosionShipView;
        private Point[] destinationPoints;
        private double angle;
        private int x;
        private int height;
        public Ship(Wave baseWave,int x ,int height, Image shipView, Image afterExplosionShipView,int sinkWay)
        {
            this.sinkWay = sinkWay;
            this.x = x;
            this.height = height;
            this.onFire = false;
            this.baseWave = baseWave;
            this.position = new Point(x,(int)(baseWave.Height-height+baseWave.WaveHeight*Math.Sin(baseWave.Softness*x+baseWave.SumDX)));
            this.shipView = shipView;
            this.afterExplosionShipView = afterExplosionShipView;
            this.destinationPoints = new Point[3];
            angle=Math.Atan(baseWave.WaveHeight*Math.Sin(baseWave.Softness*(position.X+100)+baseWave.SumDX)/100);
            destinationPoints[0] = position;
            destinationPoints[1] = new Point((int)(position.X + 100 * Math.Cos(angle)), (int)(position.Y + 100 * Math.Sin(angle)));
            destinationPoints[2] = new Point((int)(position.Y - 50 * Math.Sin(angle)), (int)(position.Y + 50 * Math.Cos(angle)));
        }
        public void PlaneHit(Plane p)
        {
            if ((p.RightDownPosition.Y > position.Y) & (p.RightDownPosition.Y < position.Y + 50) & (p.RightDownPosition.X> position.X) & (p.RightDownPosition.X< position.X + 100))
            {
                p.PlaneBurnOccured(p);
                if (!onFire)
                {
                    ShipHit(this);
                    onFire = true;
                }
            }
        }
        public void BombHit(Bomb b)
        {
            if (b.Position.Y > position.Y & b.Position.Y < position.Y + 50 & b.Position.X > position.X & b.Position.X < position.X + 100)
            {
                b.ExplosionOccured(b);
                if (!onFire)
                {
                    ShipHit(this);
                    onFire = true;
                }
            }
        }
        public void OnPaint(Graphics g)
        {
            if (onFire)
            { g.DrawImage(afterExplosionShipView, destinationPoints); }
            else
            { g.DrawImage(shipView, destinationPoints); }
        }
        public void Move()
        {
            if (!onFire)
            {
                angle = Math.Atan(baseWave.WaveHeight * Math.Sin(baseWave.Softness * (position.X + 100) + baseWave.SumDX) / 100);
                position = new Point(x, (int)(baseWave.Height - height + baseWave.WaveHeight * Math.Sin(baseWave.Softness * x + baseWave.SumDX)));
                destinationPoints[0] = position;
                destinationPoints[1] = new Point((int)(position.X + 100 * Math.Cos(angle)), (int)(position.Y + 100 * Math.Sin(angle)));
                destinationPoints[2] = new Point((int)(position.X - 50 * Math.Sin(angle)), (int)(position.Y + 50 * Math.Cos(angle)));
            }
            else
            {
                if (sinkWay == 0)
                {
                    angle = angle + 0.02;
                    position = new Point((int)(position.X + 1), (int)(position.Y + 1));
                    destinationPoints[0] = position;
                    destinationPoints[1] = new Point((int)(position.X + 100 * Math.Cos(angle)), (int)(position.Y + 100 * Math.Sin(angle)));
                    destinationPoints[2] = new Point((int)(position.X - 50 * Math.Sin(angle)), (int)(position.Y + 50 * Math.Cos(angle)));
                }
                if (sinkWay == 1)
                {
                    position = new Point(x, (int)(position.Y + 1));
                    destinationPoints[0] = position;
                    destinationPoints[1] = new Point((int)(position.X + 100 * Math.Cos(angle)), (int)(position.Y + 100 * Math.Sin(angle)));
                    destinationPoints[2] = new Point((int)(position.X - 50 * Math.Sin(angle)), (int)(position.Y + 50 * Math.Cos(angle)));
                }
                if (sinkWay == 2)
                {
                    angle = angle - 0.02;
                    position = new Point((int)(position.X - 1), (int)(position.Y + 2));
                    destinationPoints[0] = position;
                    destinationPoints[1] = new Point((int)(position.X + 100 * Math.Cos(angle)), (int)(position.Y + 100 * Math.Sin(angle)));
                    destinationPoints[2] = new Point((int)(position.X - 50 * Math.Sin(angle)), (int)(position.Y + 50 * Math.Cos(angle)));
                }
            }
        }
    }
}
